from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from django.contrib.auth.hashers import make_password, check_password
from django.core.urlresolvers import reverse
from we.settings import BASE_DIR
from django.core.paginator import Paginator
from . . import models
import time,os
from django.db.models import Q


# 商品分类添加
def catesadd(request):

    if request.method == 'GET':
        # 获取所有的商品分类
        # data = models.Cates.objects.all()
        data = models.Cates.objects.extra(select = {'paths':'concat(path,id)'}).order_by('paths')
     
        # 分配数据
        context = {'cateslist':data}
        # 加载模板
        return render(request,'myadmin/cates/add.html',context)
    elif request.method == 'POST':
        # print(request.POST.dict())
        try:
            # 执行添加
            ob = models.Cates()
            ob.name = request.POST.get('name')
            ob.pid = request.POST.get('pid')
            # 判断是否为顶级分类
            if ob.pid == '0':
                ob.path = '0,'
            else:
                # 获取当前分类的path
                fob = models.Cates.objects.get(id=ob.pid)
                ob.path = fob.path+ob.pid+','
            ob.save()
            return HttpResponse('<script>alert("添加成功");location.href="'+reverse('myadmin_cates_index')+'"</script>')
        except:        
            return HttpResponse('<script>alert("添加失败");location.href="'+reverse('myadmin_cates_add')+'"</script>')



# 商品分类列表
def catesindex(request):

    # 获取所有的商品分类
    # data = models.Cates.objects.all()
    # 按路经排序
    data = models.Cates.objects.extra(select = {'paths':'concat(path,id)'}).order_by('paths')
        
    # 遍历path路径中的,数量
    # for x in data:
    #     a = x.path.count(',')-1
    #     print(a)
    types = request.GET.get('types')
    keywords = request.GET.get('keywords')

    # 判断是否有搜索条件
    if types:
        # 判断搜索的类型
        if types == 'all':
            # 多字段的条件搜索
            data = data.filter(Q(name__contains=keywords)|Q(path__contains=keywords))
        else:
            # 按指定字段搜索
            search = {types+'__contains':keywords}
            data = data.filter(**search )
    # 实例化分页类  参数1是所有的数据对象,参数2每页显示条数
    paginator = Paginator(data,7)

    # h获取当前页码数 ?p=n
    p =request.GET.get('p',1) 

    # 获取当前页的数据
    userlist = paginator.page(p)

    # 分配数据
    context = {'cateslist':userlist}

    

    # 加载模板
    return render(request,'myadmin/cates/index.html',context)




# 商品分类删除
def catesdel(request):
    # 分类的id
    cid = request.GET.get('cid')

    # 获取分类对象
    ob = models.Cates.objects.get(id=cid)
    # 判断当前类下是否还有子类
    num = models.Cates.objects.filter(pid=cid).count()
    if num :
        # 有子类,不能删除
        return JsonResponse({'error':1,'msg':'有子类,不删除'})

    # 确认类下是否有商品发布
    # 获取分类对象
    ob = models.Cates.objects.get(id=cid)
    ob.delete()
    return JsonResponse({'error':0,'msg':'成功删除'})


# 分类ajax修改
def catesedit(request):
    try:
        # 根据cid获取分类对象
        ob = models.Cates.objects.get(id=request.GET.get('cid'))
        ob.name = request.GET.get('name')
        ob.save()

        return JsonResponse({'error':0,'mag':'更新成功'})
    except:        
        return JsonResponse({'error':1,'mag':'更新失败'})





















